package dao.Impl;

import Util.DataUtil;
import bean.CClass;
import dao.classDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class classDaoImpl implements classDao {

    @Override
    public List<CClass> classlist() {
        return null;
    }

    @Override
    public List<CClass> ListByPage(int pageNo, int pageSize, String cClass) {
        List<CClass> list = new ArrayList<>();
        Connection conet = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conet = DataUtil.getConn();
            String sql = "select * from c_class where 1=1";
            if (cClass != null && !cClass.equalsIgnoreCase("")) {
                sql += " and c_name like '%" + cClass + "%'";
            }
            sql += " limit " + (pageNo - 1) * pageSize + "," + pageSize;
            ps = conet.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                CClass cc = new CClass();
                cc.setCName(rs.getString("c_name"));
                cc.setCRes(rs.getString("c_res"));
                cc.setCCel(rs.getString("c_cel"));
                cc.setCDis(rs.getString("c_dis"));
                cc.setCId(rs.getLong("c_id"));
                list.add(cc);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DataUtil.close(rs, ps, conet);
        }

        return list;
    }

    @Override
    public boolean add(CClass cClass) {
        int count = 0;
        Connection conet = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        boolean flag = false;
        try {
            conet = DataUtil.getConn();
            String sql = "insert into c_class(c_name,c_res,c_cel,c_dis) value(?,?,?,?)";
            ps = conet.prepareStatement(sql);
            ps.setString(1,cClass.getCName());
            ps.setString(2,cClass.getCRes());
            ps.setString(3,cClass.getCCel());
            ps.setString(4,cClass.getCDis());
            count = ps.executeUpdate();
            if (count != 0) {
                flag = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DataUtil.close(rs, ps, conet);
        }
        return flag;
    }

    @Override
    public boolean delete(CClass cClass) {
        return false;
    }

    @Override
    public CClass find(CClass cClass) {
        Connection conet = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        CClass cc =new CClass();
        System.out.println("cClass = " + cClass);
        try {
            conet = DataUtil.getConn();
            String sql = "select * from c_class where c_name = ?";
            ps = conet.prepareStatement(sql);
            ps.setString(1,cClass.getCName());
            rs = ps.executeQuery();
            System.out.println(cClass.getCName());
            while (rs.next()){
                cc.setCId(rs.getInt("c_id"));
                cc.setCDis(rs.getString("c_dis"));
                cc.setCCel(rs.getString("c_cel"));
                cc.setCName(rs.getString("c_name"));
                cc.setCRes(rs.getString("c_res"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return cc;
    }

    @Override
    public boolean altArea(CClass cClass) {
        Connection conet = null;
        PreparedStatement ps = null;
        boolean flag = false;
        try {
            conet = DataUtil.getConn();
            String sql = "update t_area set fk_ClassId = ? where f_areaName = ?";
            ps = conet.prepareStatement(sql);
            ps.setLong(1,cClass.getCId());
            ps.setString(2,cClass.getCDis());
            int count = ps.executeUpdate();
            if(count!=0){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }


    @Override
    public int getListCount(String cClass) {
        int count = 0;
        Connection conet = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conet = DataUtil.getConn();
            String sql = "select count(0) from c_class where 1=1";
            if (cClass != null && cClass.equalsIgnoreCase("")) {
                sql += " and insect like '%" + cClass + "%'";
            }
            ps = conet.prepareStatement(sql);
            rs = ps.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DataUtil.close(rs, ps, conet);
        }
        return count;
    }

    @Override
    public boolean alter(CClass cClass) {
        Connection conet = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        boolean flag = false;
        try {
            conet = DataUtil.getConn();
            String sql ="update c_class set c_res=?,c_cel=? where c_id = ?";
            ps = conet.prepareStatement(sql);
            ps.setString(1,cClass.getCRes());
            ps.setString(2,cClass.getCCel());
            ps.setLong(3,cClass.getCId());
            count = ps.executeUpdate();
            if(count!=0){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }
}
